using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using AvansRS.Domain;
using AvansRS.Domain.DataInterface;

namespace AvansRS.Web.Controllers
{
    public class EventController : Controller
    {
        IRepository<Location> mLocationRepository;
        IRepository<Event> mEventRepository;
        IRepository<Person> mPersonRepository;
        IRepository<Academy> mAcademyRepository;
        IRepository<Study> mStudyRepository;
        


        public EventController(IRepository<Event> eventRepository, IRepository<Location> locationRepository,
            IRepository<Academy> academyRepository, IRepository<Study> studyRepository, IRepository<Person> personRepository)
        {
            mEventRepository = eventRepository;
            mLocationRepository = locationRepository;
            mAcademyRepository = academyRepository;
            mStudyRepository = studyRepository;
            mPersonRepository = personRepository;
        }

        //
        // GET: /Event/

        public ActionResult Index()
        {
            IList<Event> events = mEventRepository.GetAll();
            return View(events);
        }

        //
        // GET: /Event/EventCreate

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EventCreate()
        {
            SelectList locationList = new SelectList(mLocationRepository.GetAll(), "ID", "Name");
            ViewData["locationList"] = locationList;

            return View();
        }

        //
        // POST: /Event/LocationCreate

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EventCreate([Bind(Exclude = "ID")] Event ev, int locationID)
        {
            if (!ModelState.IsValid)
            {
                SelectList locationList = new SelectList(mLocationRepository.GetAll(), "ID", "Name");
                ViewData["locationList"] = locationList;
                return View();
            }

            Location loc = mLocationRepository.GetByID(locationID);
            ev.Location = loc;
            mEventRepository.Add(ev);

            TempData["Message"] = "Event '" + ev.Name + "' toegevoegd.";
            return RedirectToAction("Index", "Event");
        }

        //
        // GET: /Event/EventEdit/5

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EventEdit(int id)
        {
            Event ev = mEventRepository.GetByID(id);
            SelectList locationList = new SelectList(mLocationRepository.GetAll(), "ID", "Name", ev.Location.ID);
            ViewData["locationList"] = locationList;

            return View(ev);
        }

        //
        // POST: /Event/EventEdit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EventEdit(Event ev, int locationID)
        {
            if (!ModelState.IsValid)
                return View(mEventRepository.GetByID(ev.ID));

            Event evRep = mEventRepository.GetByID(ev.ID);
            Location loc = mLocationRepository.GetByID(locationID);
            ev.Location = loc;
            mEventRepository.Save(ev);


            TempData["Message"] = "Event met naam '" + evRep.Name + "' gewijzigd naar '" + ev.Name + "'.";
            return RedirectToAction("Index", "Event");
        }

        //
        // GET: /Event/EventDetails/5

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EventDetails(int id)
        {
            Event ev = mEventRepository.GetByID(id);
            return View(ev);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult EventDelete(int id)
        {
            Event ev = mEventRepository.GetByID(id);
            return View(ev);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EventDelete(Event ev)
        {
            mEventRepository.Delete(ev);
            //mLocationRepository.Save();

            TempData["Message"] = "Event met naam '" + ev.Name + "' verwijderd.";
            return RedirectToAction("Index", "Event");
        }


        //LOCATION

        //
        // GET: /Event/LocationIndex

        public ActionResult LocationIndex()
        {
            //ViewData["Message"] = "a";
            IList<Location> locations = mLocationRepository.GetAll();
            return View(locations);
        }

        //
        // GET: /Event/LocationCreate

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult LocationCreate()
        {
            return View();
        }

        //
        // POST: /Event/LocationCreate
        
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult LocationCreate([Bind(Exclude = "ID")] Location location)
        {
            if (!ModelState.IsValid)
                return View();

            mLocationRepository.Add(location);

            TempData["Message"] = "Locatie '"+location.Name+"' toegevoegd.";
            return RedirectToAction("LocationIndex", "Event");  
  
        }

        //
        // GET: /Event/LocationEdit/5

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult LocationEdit(int id)
        {
            Location location = mLocationRepository.GetByID(id);
            return View(location);
        }

        //
        // POST: /Event/LocationEdit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult LocationEdit(Location location)
        {
            if (!ModelState.IsValid)
                return View(mLocationRepository.GetByID(location.ID));

            Location l = mLocationRepository.GetByID(location.ID);
            mLocationRepository.Save(location);


            TempData["Message"] = "Locatie met naam '" + l.Name + "' gewijzigd naar '" + location.Name + "'.";
            return RedirectToAction("LocationIndex", "Event"); 
        }

        //
        // GET: /Event/LocationDetails/5

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult LocationDetails(int id)
        {
            Location location = mLocationRepository.GetByID(id);
            return View(location);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult LocationDelete(int id)
        {
            Location location = mLocationRepository.GetByID(id);
            return View(location);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult LocationDelete(Location location)
        {
            mLocationRepository.Delete(location);
            //mLocationRepository.Save();

            TempData["Message"] = "Locatie met naam '" + location.Name + "' verwijderd.";
            return RedirectToAction("LocationIndex", "Event"); 
        }

        //
        // GET: /Rooster/RoosterCreate

        private MultiSelectList GetPerons(string[] selectedValues)
        {
            IList<Person> perons = mPersonRepository.GetAll();

            return new MultiSelectList(perons, "ID", "sName", selectedValues);
        }

        //
        // GET: /Event/RegistrationManage

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult RegistrationManage(int id)
        {
            Event ev = mEventRepository.GetByID(id);
            SelectList academyList = new SelectList(mAcademyRepository.GetAll());
            ViewData["academylist"] = academyList;
            SelectList studylist = new SelectList(mStudyRepository.GetAll());
            ViewData["studylist"] = studylist;
            ViewData["personlist"] = GetPerons(null);
            
            return View(ev);
        }

        //
        // POST: /Event/RegistrationManage

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult RegistrationManage([Bind(Exclude = "ID")] Event ev, int eventID, string[] Perons)
        {
            if (!ModelState.IsValid)
            {
                Event eve = mEventRepository.GetByID(eventID);
                ViewData["personlist"] = GetPerons(null);
                return View(eve);
            }

            Event tmpEvent = mEventRepository.GetByID(eventID);

           // IList<Registration> reg = new List<Registration>();
            IList<Entity> e = new List<Entity>();
            foreach (string sT in Perons)
            {
                Person t = mPersonRepository.GetByID(Int32.Parse(sT));
                e.Add(t.Entity);
            }

            tmpEvent.ApplicableEntities = e;
            mEventRepository.Save(tmpEvent);

            TempData["Message"] = "Registrations voor '" + ev.Name + "' toegevoegd.";
            return RedirectToAction("Index", "Event");
        }
    }
}
